package com.example.readbook.Utils

import android.annotation.SuppressLint
import android.content.Context
import com.example.readbook.MyApp

@SuppressLint("CommitPrefEdits")
/**
 * Sp工具类
 * Created by Administrator on 2018/5/3.
 */
object SpUtils {
    private const val tag = "-1"

    private val Sp by lazy {
        MyApp.mContext.getSharedPreferences("yxsSp", Context.MODE_PRIVATE)
    }
    private val Edit by lazy {
        Sp.edit()
    }

    fun putBoolean(name: String, msg: Boolean): Boolean = Edit.putBoolean(name, msg).commit()

    fun getBoolean(name: String, default: Boolean = false): Boolean = Sp.getBoolean(name, default)

    fun putString(name: String, msg: String): Boolean = Edit.putString(name, msg).commit()

    fun getString(name: String, default: String = tag): String = Sp.getString(name, default)!!

    fun putInt(name: String, msg: Int): Boolean = Edit.putInt(name, msg).commit()

    fun getInt(name: String, default: Int = tag.toInt()): Int = Sp.getInt(name, default)

    fun putLong(name: String, msg: Long): Boolean = Edit.putLong(name, msg).commit()

    fun getLong(name: String, default: Long = tag.toLong()): Long = Sp.getLong(name, default)

    fun put(name: String, msg: Any) {
        when (msg) {
            is String -> putString(name, msg)
            is Int -> putInt(name, msg)
            is Boolean -> putBoolean(name, msg)
            is Long -> putLong(name, msg)
        }
    }

    fun get(name: String, default: Any): Any = when (default) {
        is String -> putString(name, default)
        is Int -> putInt(name, default)
        is Boolean -> putBoolean(name, default)
        is Long -> putLong(name, default)
        else -> 999
    }

    fun remove(name: String) = Edit.remove(name).commit()

}